package com.renjun.smweb.controller.SchoolController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.exceptions.BaseException;
import com.renjun.smweb.service.SchoolService.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by huamin on 2017/2/21.
 * 学校管理M1
 */
@Controller
@RequestMapping("/schoolmgr")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    /**
     * 新增学校/添加学校
     * @author lrs
     * @param request params:{schoolcode:学校代码 , schoolname:学校名称 , address:地址 , zipcode:邮政编码 , orgid:组织机构码 , faxnum:传真 , schooltelephone:学校电话 , schoolurl:学校网站 , districtcode:区县代码 , jxny:建校年月 , xxlb: 学校类别, xxbb:学校办别 , bxdj:办学等级 , prename:校长姓名 , pretel:校长电话 , tbrs:托班人数 , tbbs:托班班数 , xbrs:小班人数 , xbbs:小班班数}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/addSchool",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-1", desc = "新增学校/添加学校")
    public JSONObject addSchool(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.addSchool(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 注销学校/删除学校
     * @author lrs
     * @param request params:{schoolcode:学校代码}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/cancelSchool",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-2", desc = "注销学校/删除学校")
    public JSONObject cancelSchool(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.cancelSchool(params);
        } catch (BaseException | SQLException e) {
            e.printStackTrace();
//            rs.put("message",e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 修改学校/学校修改
     * @author lrs
     * @param request params:{schoolcode:学校代码 , schoolname:学校名称 , address:地址 , zipcode:邮政编码 , orgid:组织机构码 , faxnum:传真 , schooltelephone:学校电话 , schoolurl:学校网站 , districtcode:区县代码 , jxny:建校年月 , xxlb: 学校类别, xxbb:学校办别 , bxdj:办学等级 , prename:校长姓名 , pretel:校长电话 , tbrs:托班人数 , tbbs:托班班数 , xbrs:小班人数 , xbbs:小班班数}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/modifySchool",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-3", desc = "修改学校/学校修改")
    public JSONObject modifySchool(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.modifySchool(params);
        } catch (Exception e) {
            e.printStackTrace();
//            rs.put("message",e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 查看学校/学校详情
     * @author lrs
     * @param request params:{schoolcode:学校代码}
     * @param response data:{schoolcode:学校代码 , schoolname:学校名称 , address:地址 , zipcode:邮政编码 , orgid:组织机构码 , faxnum:传真 , schooltelephone:学校电话 , schoolurl:学校网站 , districtcode:区县代码 , jxny:建校年月 , xxlb: 学校类别, xxbb:学校办别 , bxdj:办学等级 , prename:校长姓名 , pretel:校长电话 , tbrs:托班人数 , tbbs:托班班数 , xbrs:小班人数 , xbbs:小班班数}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/getSchool",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-4", desc = "查看学校/学校详情")
    public JSONObject getSchool(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.getSchool(params);
        } catch (Exception e) {
            e.printStackTrace();
//            rs.put("message",e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 查询学校/学校列表
     * @author lrs
     * @param request params:{}
     * @param response data:{schoolcode:学校代码 , schoolname:学校名称 , xxbb:学校办别 , bxdj:办学等级 , deleted:学校状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/querySchool",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-5", desc = "查询学校/学校列表")
    public JSONObject querySchool(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.querySchool(params);
        } catch (Exception e) {
            e.printStackTrace();
//            rs.put("message",e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 导出学校/学校导出
     * @author lrs
     * @param request params:{}
     * @param response data:{url:下载地址 , message:请求描述 , status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/expSchool",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-6", desc = "导出学校/学校导出")
    public JSONObject expSchool(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.expSchool(params);
        } catch (Exception e) {
            e.printStackTrace();
//            rs.put("message",e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 根据区县获取学校
     * @author lrs
     * @param request params:{districtcode:区县代码}
     * @param response data:{districtcode:区县代码 , schooolcode:学校代码 , schoolname:学校名称}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/getSchoolByDistrict",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-7", desc = "根据区县获取学校")
    public JSONObject getSchoolByDistrict(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
//            rs = schoolService.getSchoolByDistrict(params);
            rs = schoolService.getSchoolByDistrictRedis(params);
        } catch (Exception e) {
            e.printStackTrace();
//            rs.put("message",e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 恢复学校/学校恢复
     * @author lrs
     * @param request params:{schoolcode:学校代码}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/recoverSchool",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-8", desc = "恢复学校/学校恢复")
    public JSONObject recoverSchool(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.recoverSchool(params);
        } catch (BaseException | SQLException e) {
            e.printStackTrace();
//            rs.put("message",e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 学校招生片区
     * @author hm
     * @param request params:{schoolcode:学校代码}
     * @param response data:{map:[{"city":"","qx":"","xx":"","dz":"","xxjd":"","xxjw":"","lng":"","lat":"","jwinfo":[{"zsjd":"","zsjw":"","lng":"","lat":""},{"zsjd":"","zsjw":"","lng":"","lat":""}]}]}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/querySchoolDistrict",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M1-9", desc = "学校招生片区")
    public JSONObject querySchoolDistrict(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = schoolService.querySchoolDistrict(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }
}
